package com.ruoyi.framework.config;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.bstek.uflo.env.EnvironmentProvider;
import com.ruoyi.common.utils.security.ShiroUtils;

@Configuration
public class UfloEnvironmentProvider implements EnvironmentProvider {
	
    @Resource(name = "ufloSessionFactory")
    private SessionFactory sessionFactory;
    
    @Resource(name = "ufloTransactionManager")
    private PlatformTransactionManager platformTransactionManager;

    @Override
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public PlatformTransactionManager getPlatformTransactionManager() {
        return platformTransactionManager;
    }

    public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) {
        this.platformTransactionManager = platformTransactionManager;
    }

    @Override
    public String getLoginUser() {
        return ShiroUtils.getSysUser().getUserName();
    }

    @Override
    public String getCategoryId() {
        return null;
    }

    @Bean("ufloTransactionManager")
    public HibernateTransactionManager ufloTransactionManager(SessionFactory sessionFactory){
        HibernateTransactionManager hi = new HibernateTransactionManager();
        hi.setSessionFactory(getSessionFactory());
        return hi;
    }

}